<?php

namespace App\Http\Controllers\Home;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class TablesController extends BaseController
{
    public function install(){
        $this->create();
        ini_set('output_buffering', 0);
        $data = [];
//        for($j=4;$j<=6;$j++){
//            for($i=10000*$j;$i<10000*($j+1);$i++){
//                $data[$i]['event'] = '23'.$i;
//                $data[$i]['type'] = 1;
//                $data[$i]['addtime'] = date('Y-m-d H:i:s');
//            }
////            sleep(2);
//            DB::table('ih_log')->insert($data);
//        }
    }
    public function create(){
        $tables = DB::select('show tables');
        $object = json_decode(json_encode($tables),true);
        $names = [];
        foreach($object as $k => $v){
            $names[] = $v['Tables_in_laravel'];
        }
        $newtable[] = 'ih_log_0';
        $newtable[] = 'ih_log_1';
        if(!in_array($newtable,$names)){
            foreach ($newtable as $v){
                Schema::dropIfExists($v);
                Schema::create($v, function ($table) {
                    $table->increments('id',1);
                    $table->char('event',225);
                    $table->integer('type')->unsigned();
                    $table->timestamps();
                });
            }
        }
        //INSERT INTO ih_log_0 (`event`,`type`) SELECT event,type FROM ih_log where event LIKE '%1'查询个位数等于1的
        //查询ih_log中的所有偶数
        $list1 = DB::select('select event,type from ih_log where mod(event,2)= 0');
        $data1 = [];
        foreach ($list1 as $k=>$v){
            $data1[$k]['event'] = $v->event;
            $data1[$k]['type'] = $v->type;
            $data1[$k]['created_at'] = date("Y-m-d");
        }
        $res = array_chunk($data1,1000);
        foreach ($res as $v){
            DB::table('ih_log_0')->insert($v);
        }
        $list2 = DB::select('select event,type from ih_log where mod(event,2)= 1');
        $data2= [];
        foreach ($list2 as $k=>$v){
            $data2[$k]['event'] = $v->event;
            $data2[$k]['type'] = $v->type;
            $data2[$k]['created_at'] = date("Y-m-d");
        }
        $res2 = array_chunk($data2,1000);
        foreach ($res2 as $v){
            DB::table('ih_log_1')->insert($v);
        }

    }
}
